package com.example.shellcollector.database;

import android.content.Context;

import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;

import com.example.shellcollector.Entity.AppDailyUsage;
import com.example.shellcollector.Entity.InstalledAppUsage;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Database(entities = { InstalledAppUsage.class, AppDailyUsage.class }, version = 2, exportSchema = false)
public abstract class ShellCollectorDatabase extends RoomDatabase {

    public abstract InstalledAppUsageDao getInstalledAppUsageDao();
    public abstract AppDailyUsageDao getAppDailyUsageDao();

    private static volatile ShellCollectorDatabase INSTANCE;
    private static final int NUMBER_OF_THREADS = 4;
    public static final ExecutorService databaseWriteExecutor =
            Executors.newFixedThreadPool(NUMBER_OF_THREADS);

    public static ShellCollectorDatabase getDatabase(final Context context) {
        if (INSTANCE == null) {
            synchronized (ShellCollectorDatabase.class) {
                if (INSTANCE == null) {
                    INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
                            ShellCollectorDatabase.class, "shell_collector_database")
                            .allowMainThreadQueries() // queries in this app is simple...
                            .fallbackToDestructiveMigration()
                            .build();
                }
            }
        }
        return INSTANCE;
    }
}
